<?php

/**
 * Examples
 *
 * Some usage examples on History.
 *
 * @licence 	MIT Licence
 * @category	Models
 * @author  	Simon Stenhouse
 * @link    	http://stensi.com
 */
class Examples extends Controller {

	/**
	 * Constructor
	 *
	 * Initialize Examples.
	 */
	function Examples()
	{
		parent::Controller();

		// To help with links
		$this->load->helper('url');

		// In case you didn't setup the History library to be autoloaded
		$this->load->library('history');

		// Just for this example, we'll set the default page as this Controllers index
		$this->history->default_page = 'examples';
	}
	
	// --------------------------------------------------------------------

	/**
	 * Index
	 */
	function index()
	{
		echo '<h1>History Example</h1>';

		// Just for this example, we'll clear the history on this page.
		$this->history->delete($this->history->length);

		$this->_common();

		echo "<strong>Note:</strong> We're clearing the history when you go to this page.";
	}

	// --------------------------------------------------------------------

	/**
	 * Alpha
	 */
	function alpha()
	{
		echo '<h1>Alpha</h1>';

		$this->_common();
	}

	// --------------------------------------------------------------------

	/**
	 * Bravo
	 */
	function bravo()
	{
		echo '<h1>Bravo</h1>';

		$this->_common();
	}

	// --------------------------------------------------------------------

	/**
	 * Charlie
	 */
	function charlie()
	{
		echo '<h1>Charlie</h1>';

		$this->_common();
	}

	// --------------------------------------------------------------------

	/**
	 * Delta
	 */
	function delta()
	{
		echo '<h1>Delta</h1>';

		// Delete this page from the history
		$this->history->delete();

		$this->_common();
	}

	// --------------------------------------------------------------------

	/**
	 * Back
	 */
	function back($pages = 1)
	{
		// Go to Previous Page
		$this->history->back($pages);
	}

	// --------------------------------------------------------------------

	/**
	 * Common
	 */
	function _common()
	{
		// Uncomment the below line if you have auto saving disabled, and want to do it manually
		// $this->history->save();
		
		echo "<p>Click the links to build up your history. You'll notice the page won't be saved if you go to the same page more than once.</p>";

		echo '<hr />';

		echo '<ul>';
		echo '<li><a href="' . site_url('examples/alpha') . '">Alpha</a></li>';
		echo '<li><a href="' . site_url('examples/bravo') . '">Bravo</a></li>';
		echo '<li><a href="' . site_url('examples/charlie') . '">Charlie</a></li>';
		echo '<li><a href="' . site_url('examples/delta') . '">Delta</a> (this page is setup to not be saved into the history)</li>';
		echo '</ul>';

		echo '<hr />';

		echo '<p><a href="' . site_url('examples/back') . '">Go to Previous Page</a> (';

		// Echo URL of previous page
		echo $this->history->get(1);

		echo ') | <a href="' . site_url('examples/back/2') . '">Go to Back 2 Pages</a> (';

		// Echo URL of page 2 pages back
		echo $this->history->get(2);

		echo ')</p>';

		echo '<hr />';

		// Get history session and reverse it as I'll be showing it in order of most recent to oldest
		$history = array_reverse($this->session->userdata('history'));

		echo '<ul>';

		for ($i = 0; $i < count($history); $i++)
		{
			echo '<li>';

			echo $history[$i];

			switch ($i)
			{
				case 0:	echo ' (current page)'; break;
				case 1:	echo ' (previous page)'; break;
				case 2:	echo ' (back 2 pages)'; break;
			}			

			echo '</li>';
		}

		echo '</ul>';
	}
}

/* End of file examples.php */
/* Location: ./application/controllers/examples.php */